/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.database;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import model.entities.Account;
import model.entities.Book;
import model.entities.Folder;

/**
 *
 * @author HoangTien
 */
public class SqlFolder {
    MyConnection mc = new MyConnection();
    
    public int insertBookToFolder(int folderId, int bookId) {
        List para = new ArrayList();
        para.add(bookId);
        para.add(folderId);
        try {
            return mc.executeUpdate("sp_insert_book_to_folder", para);
        } catch (Exception e) {
            
        }
        
        return -1;
    }
    
    public int createFolder(Account acc, String title) {
        int rows = 0;
        List para = new ArrayList();
        para.add(acc.getUsername());
        para.add(title);
        try {
            return mc.executeUpdate("sp_insert_folder",para);           
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }
    
    public List<Folder> getListFolder(Account account ) {
        Folder folder;
        List<Folder> data = new ArrayList<Folder>();
        SqlBook sqlBook = new SqlBook();
        List para = new ArrayList();
        para.add(account.getUsername()); 
        try {
            ResultSet rs = mc.executeQuery("sp_get_folder_by_uname", para);
            while(rs.next()) {
                folder = new Folder();
                folder.setFolderId(rs.getInt(1));
                folder.setUsername(rs.getString(2));
                folder.setFolderTitle(rs.getString(3));
                folder.setListBooks(sqlBook.getListBooks(folder));
                data.add(folder); 
            }
        } catch (Exception e) {
            e.printStackTrace();
        }  
        return data;
    } 
}
